package cc.solart.wavesidebar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.fastjson.JSON;

import java.util.Collections;
import java.util.List;

import cc.solart.turbo.OnItemClickListener;
import cc.solart.wave.R;
import cc.solart.wave.WaveSideBarView;
import cc.solart.wavesidebar.adapter.CityAdapter;
import cc.solart.wavesidebar.bean.City;

public class CityPickerActivity extends AppCompatActivity {

    public static final String EXTRA_RESULT_CITY_NAME = "extra_result_city_name";

    private RecyclerView mRecyclerView;
    private WaveSideBarView mSideBarView;
    private CityAdapter adapter;

    public static void startActivity(Activity activity, int requestCode) {

        Intent intent = new Intent(activity, CityPickerActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }

    public static void startActivity(Fragment fragment, int requestCode) {

        Intent intent = new Intent(fragment.getActivity(), CityPickerActivity.class);
        fragment.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_picker);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mSideBarView = (WaveSideBarView) findViewById(R.id.side_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        final PinnedHeaderDecoration decoration = new PinnedHeaderDecoration();
        decoration.registerTypePinnedHeader(1, new PinnedHeaderDecoration.PinnedHeaderCreator() {
            @Override
            public boolean create(RecyclerView parent, int adapterPosition) {
                return true;
            }
        });
        mRecyclerView.addItemDecoration(decoration);


        new Thread(new Runnable() {
            @Override
            public void run() {

                final List<City> list = JSON.parseArray(City.DATA, City.class);
                Collections.sort(list, new LetterComparator());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new CityAdapter(CityPickerActivity.this, list);
                        mRecyclerView.setAdapter(adapter);
                        adapter.addOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onItemClick(RecyclerView.ViewHolder vh, int position) {

                                Intent intent = new Intent();
                                intent.putExtra(EXTRA_RESULT_CITY_NAME, adapter.getItem(position).name);
                                setResult(RESULT_OK, intent);
                                finish();
                            }
                        });

                    }
                });
            }
        }).start();

        mSideBarView.setOnTouchLetterChangeListener(new WaveSideBarView.OnTouchLetterChangeListener() {
            @Override
            public void onLetterChange(String letter) {
                int pos = adapter.getLetterPosition(letter);

                if (pos != -1) {
                    mRecyclerView.scrollToPosition(pos);
                    LinearLayoutManager mLayoutManager =
                            (LinearLayoutManager) mRecyclerView.getLayoutManager();
                    mLayoutManager.scrollToPositionWithOffset(pos, 0);
                }
            }
        });
    }


}
